package com.youlu.campus.admin.commission;


import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.commission.service.DistributorChannelAdminService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.entity.commission.dto.*;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分销渠道控制器
 */
@RestController
public class DistributorChannelController {

    @Autowired
    private DistributorChannelAdminService distributorChannelAdminService;

    @Autowired
    private RoleService roleService;

    /**
     * 获取渠道列表
     * @param dto
     * @return
     */
    @PostMapping("/distributor/channel/list")
    public QResult list(@RequestBody DistributorChannelSearchDTO dto) {
        String userId = AuthUtils.getUserId();
        dto.setAccountId(userId);
        dto.setChannelUser(isChannel(userId));
        return QResult.success(distributorChannelAdminService.list(dto));
    }

    /**
     * 保存分销渠道信息
     * @param dto
     * @return
     */
    @PostMapping("/distributor/channel/save")
    public QResult updateStatus(@RequestBody @Validated DistributorChannelSaveDTO dto) {
        String userId = AuthUtils.getUserId();
        dto.setChannelUser(isChannel(userId));
        return QResult.success(distributorChannelAdminService.save(dto));
    }

    public boolean isChannel(String userId) {
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> "渠道分销".equals(role.getName())).count();
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取渠道详情
     * @param dto
     * @return
     */
    @PostMapping("/distributor/channel/detail")
    public QResult getChannelDetail(@RequestBody @Validated DistributorChannelDetailDTO dto) {
        return QResult.success(distributorChannelAdminService.getDetail(dto));
    }

    /**
     * 渠道提现
     * @param dto
     * @return
     */
    @PostMapping("/distributor/channel/withdraw/apply")
    public QResult channelWithdraw(@RequestBody @Validated DistributorChannelWithdrawDTO dto) {
        String username = AuthUtils.getLoginName();
        String userId = AuthUtils.getUserId();
        dto.setOperator(username);
        dto.setChannelUser(isChannel(userId));
        dto.setAccountId(userId);
        return QResult.success(distributorChannelAdminService.channelWithdraw(dto));
    }

    /**
     * 渠道提现列表
     * @param dto
     * @return
     */
    @PostMapping("/distributor/channel/withdraw/list")
    public QResult withdrawList(@RequestBody @Validated DistributorChannelWithdrawSearchDTO dto) {
        String userId = AuthUtils.getUserId();
        dto.setAccountId(userId);
        dto.setChannelUser(isChannel(userId));
        return QResult.success(distributorChannelAdminService.withdrawList(dto));
    }
}
